{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Dos Arquitecturas de CNN para Reconocimiento de Fisuras e Imperfecciones en Superficies\n",
    "\n",
    "***\n",
    "\n",
    "## Descripción\n",
    "\n",
    "Componente para definir la arquitectura y entrenamiento de una CNN para reconocimiento de fisuras e imperfecciones en superficies, como se describe en [1].\n",
    "Además de la implementación propuesta en el paper, que es para clasificar más de una clase de imperfecciones: \"fisura\", \"grieta\", \"ralladura\" se propone una arquitectura más sencilla, que puede ser entrenada en una CPU.\n",
    "Cabe aclarar que sólo se dispone de dataset para entrenar un modelo binario [2].\n",
    "\n",
    "\n",
    "### Limitaciones de la Versión CPU Friendly\n",
    "\n",
    "- Sólo distingue dos clases: positivo (hay grieta) o negativo (no hay).\n",
    "- Sólo tiene dos capas de convolución, con kernels de 3x3.\n",
    "- Accuracy ~60%\n",
    "\n",
    "\n",
    "## Referencias\n",
    "\n",
    "1. <cite data-cite=\"tie-2764844-pp\">NB-CNN: Deep Learning-Based Crack Detection Using Convolutional Neural Network and Naïve Bayes Data Fusion</cite>\n",
    "2. <cite>Özgenel, Çağlar Fırat (2018), “Concrete Crack Images for Classification”, Mendeley Data, v1 http://dx.doi.org/10.17632/5y9wdsg2zt.1#file-c0d86f9f-852e-4d00-bf45-9a0e24e3b932</cite>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n"
     ]
    }
   ],
   "source": [
    "from CNNBuilderAndTrainer import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## CNN Propuesta por el paper"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "conv2d_1 (Conv2D)            (None, 110, 110, 32)      11616     \n",
      "_________________________________________________________________\n",
      "batch_normalization_1 (Batch (None, 110, 110, 32)      128       \n",
      "_________________________________________________________________\n",
      "elu_1 (ELU)                  (None, 110, 110, 32)      0         \n",
      "_________________________________________________________________\n",
      "max_pooling2d_1 (MaxPooling2 (None, 52, 52, 32)        0         \n",
      "_________________________________________________________________\n",
      "conv2d_2 (Conv2D)            (None, 42, 42, 48)        185856    \n",
      "_________________________________________________________________\n",
      "batch_normalization_2 (Batch (None, 42, 42, 48)        192       \n",
      "_________________________________________________________________\n",
      "elu_2 (ELU)                  (None, 42, 42, 48)        0         \n",
      "_________________________________________________________________\n",
      "max_pooling2d_2 (MaxPooling2 (None, 19, 19, 48)        0         \n",
      "_________________________________________________________________\n",
      "conv2d_3 (Conv2D)            (None, 15, 15, 64)        76800     \n",
      "_________________________________________________________________\n",
      "batch_normalization_3 (Batch (None, 15, 15, 64)        256       \n",
      "_________________________________________________________________\n",
      "elu_3 (ELU)                  (None, 15, 15, 64)        0         \n",
      "_________________________________________________________________\n",
      "max_pooling2d_3 (MaxPooling2 (None, 7, 7, 64)          0         \n",
      "_________________________________________________________________\n",
      "conv2d_4 (Conv2D)            (None, 3, 3, 80)          128000    \n",
      "_________________________________________________________________\n",
      "batch_normalization_4 (Batch (None, 3, 3, 80)          320       \n",
      "_________________________________________________________________\n",
      "elu_4 (ELU)                  (None, 3, 3, 80)          0         \n",
      "_________________________________________________________________\n",
      "max_pooling2d_4 (MaxPooling2 (None, 1, 1, 80)          0         \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 1, 1, 5120)        414720    \n",
      "_________________________________________________________________\n",
      "elu_5 (ELU)                  (None, 1, 1, 5120)        0         \n",
      "_________________________________________________________________\n",
      "dropout_1 (Dropout)          (None, 1, 1, 5120)        0         \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              (None, 1, 1, 96)          491616    \n",
      "=================================================================\n",
      "Total params: 1,309,504\n",
      "Trainable params: 1,309,056\n",
      "Non-trainable params: 448\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "cnn_builder = CNNBuilderAndTrainer(cnn_type=\"paper-cnn\")\n",
    "cnn_builder.build()\n",
    "cnn_builder.show_model_info()\n",
    "cnn_builder.save_model_image(\"papercnn.png\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## CNN Sencilla"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "conv2d_5 (Conv2D)            (None, 62, 62, 32)        896       \n",
      "_________________________________________________________________\n",
      "max_pooling2d_5 (MaxPooling2 (None, 31, 31, 32)        0         \n",
      "_________________________________________________________________\n",
      "conv2d_6 (Conv2D)            (None, 29, 29, 32)        9248      \n",
      "_________________________________________________________________\n",
      "max_pooling2d_6 (MaxPooling2 (None, 14, 14, 32)        0         \n",
      "_________________________________________________________________\n",
      "flatten_1 (Flatten)          (None, 6272)              0         \n",
      "_________________________________________________________________\n",
      "dense_3 (Dense)              (None, 128)               802944    \n",
      "_________________________________________________________________\n",
      "dense_4 (Dense)              (None, 2)                 258       \n",
      "=================================================================\n",
      "Total params: 813,346\n",
      "Trainable params: 813,346\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<svg height=\"556pt\" viewBox=\"0.00 0.00 217.00 556.00\" width=\"217pt\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
       "<g class=\"graph\" id=\"graph0\" transform=\"scale(1 1) rotate(0) translate(4 552)\">\n",
       "<title>G</title>\n",
       "<polygon fill=\"white\" points=\"-4,4 -4,-552 213,-552 213,4 -4,4\" stroke=\"none\"/>\n",
       "<!-- 2209724116664 -->\n",
       "<g class=\"node\" id=\"node1\"><title>2209724116664</title>\n",
       "<polygon fill=\"none\" points=\"16.5,-511.5 16.5,-547.5 192.5,-547.5 192.5,-511.5 16.5,-511.5\" stroke=\"black\"/>\n",
       "<text font-family=\"Times New Roman,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"104.5\" y=\"-525.8\">conv2d_5_input: InputLayer</text>\n",
       "</g>\n",
       "<!-- 2209429201640 -->\n",
       "<g class=\"node\" id=\"node2\"><title>2209429201640</title>\n",
       "<polygon fill=\"none\" points=\"41,-438.5 41,-474.5 168,-474.5 168,-438.5 41,-438.5\" stroke=\"black\"/>\n",
       "<text font-family=\"Times New Roman,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"104.5\" y=\"-452.8\">conv2d_5: Conv2D</text>\n",
       "</g>\n",
       "<!-- 2209724116664&#45;&gt;2209429201640 -->\n",
       "<g class=\"edge\" id=\"edge1\"><title>2209724116664-&gt;2209429201640</title>\n",
       "<path d=\"M104.5,-511.313C104.5,-503.289 104.5,-493.547 104.5,-484.569\" fill=\"none\" stroke=\"black\"/>\n",
       "<polygon fill=\"black\" points=\"108,-484.529 104.5,-474.529 101,-484.529 108,-484.529\" stroke=\"black\"/>\n",
       "</g>\n",
       "<!-- 2209724462472 -->\n",
       "<g class=\"node\" id=\"node3\"><title>2209724462472</title>\n",
       "<polygon fill=\"none\" points=\"0,-365.5 0,-401.5 209,-401.5 209,-365.5 0,-365.5\" stroke=\"black\"/>\n",
       "<text font-family=\"Times New Roman,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"104.5\" y=\"-379.8\">max_pooling2d_5: MaxPooling2D</text>\n",
       "</g>\n",
       "<!-- 2209429201640&#45;&gt;2209724462472 -->\n",
       "<g class=\"edge\" id=\"edge2\"><title>2209429201640-&gt;2209724462472</title>\n",
       "<path d=\"M104.5,-438.313C104.5,-430.289 104.5,-420.547 104.5,-411.569\" fill=\"none\" stroke=\"black\"/>\n",
       "<polygon fill=\"black\" points=\"108,-411.529 104.5,-401.529 101,-411.529 108,-411.529\" stroke=\"black\"/>\n",
       "</g>\n",
       "<!-- 2209724357208 -->\n",
       "<g class=\"node\" id=\"node4\"><title>2209724357208</title>\n",
       "<polygon fill=\"none\" points=\"41,-292.5 41,-328.5 168,-328.5 168,-292.5 41,-292.5\" stroke=\"black\"/>\n",
       "<text font-family=\"Times New Roman,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"104.5\" y=\"-306.8\">conv2d_6: Conv2D</text>\n",
       "</g>\n",
       "<!-- 2209724462472&#45;&gt;2209724357208 -->\n",
       "<g class=\"edge\" id=\"edge3\"><title>2209724462472-&gt;2209724357208</title>\n",
       "<path d=\"M104.5,-365.313C104.5,-357.289 104.5,-347.547 104.5,-338.569\" fill=\"none\" stroke=\"black\"/>\n",
       "<polygon fill=\"black\" points=\"108,-338.529 104.5,-328.529 101,-338.529 108,-338.529\" stroke=\"black\"/>\n",
       "</g>\n",
       "<!-- 2209724233768 -->\n",
       "<g class=\"node\" id=\"node5\"><title>2209724233768</title>\n",
       "<polygon fill=\"none\" points=\"0,-219.5 0,-255.5 209,-255.5 209,-219.5 0,-219.5\" stroke=\"black\"/>\n",
       "<text font-family=\"Times New Roman,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"104.5\" y=\"-233.8\">max_pooling2d_6: MaxPooling2D</text>\n",
       "</g>\n",
       "<!-- 2209724357208&#45;&gt;2209724233768 -->\n",
       "<g class=\"edge\" id=\"edge4\"><title>2209724357208-&gt;2209724233768</title>\n",
       "<path d=\"M104.5,-292.313C104.5,-284.289 104.5,-274.547 104.5,-265.569\" fill=\"none\" stroke=\"black\"/>\n",
       "<polygon fill=\"black\" points=\"108,-265.529 104.5,-255.529 101,-265.529 108,-265.529\" stroke=\"black\"/>\n",
       "</g>\n",
       "<!-- 2209724321072 -->\n",
       "<g class=\"node\" id=\"node6\"><title>2209724321072</title>\n",
       "<polygon fill=\"none\" points=\"50,-146.5 50,-182.5 159,-182.5 159,-146.5 50,-146.5\" stroke=\"black\"/>\n",
       "<text font-family=\"Times New Roman,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"104.5\" y=\"-160.8\">flatten_1: Flatten</text>\n",
       "</g>\n",
       "<!-- 2209724233768&#45;&gt;2209724321072 -->\n",
       "<g class=\"edge\" id=\"edge5\"><title>2209724233768-&gt;2209724321072</title>\n",
       "<path d=\"M104.5,-219.313C104.5,-211.289 104.5,-201.547 104.5,-192.569\" fill=\"none\" stroke=\"black\"/>\n",
       "<polygon fill=\"black\" points=\"108,-192.529 104.5,-182.529 101,-192.529 108,-192.529\" stroke=\"black\"/>\n",
       "</g>\n",
       "<!-- 2209724955896 -->\n",
       "<g class=\"node\" id=\"node7\"><title>2209724955896</title>\n",
       "<polygon fill=\"none\" points=\"52.5,-73.5 52.5,-109.5 156.5,-109.5 156.5,-73.5 52.5,-73.5\" stroke=\"black\"/>\n",
       "<text font-family=\"Times New Roman,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"104.5\" y=\"-87.8\">dense_3: Dense</text>\n",
       "</g>\n",
       "<!-- 2209724321072&#45;&gt;2209724955896 -->\n",
       "<g class=\"edge\" id=\"edge6\"><title>2209724321072-&gt;2209724955896</title>\n",
       "<path d=\"M104.5,-146.313C104.5,-138.289 104.5,-128.547 104.5,-119.569\" fill=\"none\" stroke=\"black\"/>\n",
       "<polygon fill=\"black\" points=\"108,-119.529 104.5,-109.529 101,-119.529 108,-119.529\" stroke=\"black\"/>\n",
       "</g>\n",
       "<!-- 2209724315072 -->\n",
       "<g class=\"node\" id=\"node8\"><title>2209724315072</title>\n",
       "<polygon fill=\"none\" points=\"52.5,-0.5 52.5,-36.5 156.5,-36.5 156.5,-0.5 52.5,-0.5\" stroke=\"black\"/>\n",
       "<text font-family=\"Times New Roman,serif\" font-size=\"14.00\" text-anchor=\"middle\" x=\"104.5\" y=\"-14.8\">dense_4: Dense</text>\n",
       "</g>\n",
       "<!-- 2209724955896&#45;&gt;2209724315072 -->\n",
       "<g class=\"edge\" id=\"edge7\"><title>2209724955896-&gt;2209724315072</title>\n",
       "<path d=\"M104.5,-73.3129C104.5,-65.2895 104.5,-55.5475 104.5,-46.5691\" fill=\"none\" stroke=\"black\"/>\n",
       "<polygon fill=\"black\" points=\"108,-46.5288 104.5,-36.5288 101,-46.5289 108,-46.5288\" stroke=\"black\"/>\n",
       "</g>\n",
       "</g>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cnn_builder = CNNBuilderAndTrainer(cnn_type=\"cpu-friendly-cnn\")\n",
    "cnn_builder.build()\n",
    "cnn_builder.show_model_info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
